package vista;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import util.Pair;

import modelo.TipoServicio;
import modelo.dao.Servidor;

import com.toedter.calendar.JDateChooser;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class DlgReporteTiposServicio extends javax.swing.JDialog {
	private class Chart extends JComponent {
		private ArrayList<Pair<TipoServicio, Integer>> datos;

		public Chart() {
		}

		public Dimension getPreferredSize() {
			return new Dimension(300, 300);
		}

		public void actualizar(Date desde, Date hasta) {
			datos = Servidor.serviciosPrestados()
					.consultarCantidadTipoServicio(desde == null ? null : new java.sql.Date(desde.getTime()), hasta == null ? null : new java.sql.Date(hasta.getTime()));
		}

		public void paint(Graphics g) {
			Dimension size = getSize();
			// g.fillRect(0, 0, (int) size.getWidth(), (int) size.getHeight());

			int n = datos.size();

			if (n > 0) {
				int blockWidth = (int) size.getWidth() / n;

				int maximum = 0;

				for (int i = 0; i < n; ++i)
					if (datos.get(i).second > maximum)
						maximum = datos.get(i).second;

				int lineHeight = g.getFontMetrics().getHeight(), unitHeight = (int) (size
						.getHeight() - lineHeight * 3 - 30)
						/ (maximum + 1);

				g.drawLine(30, 0, 30, (int) size.getHeight());

				for (int i = 0; i <= maximum; ++i) {
					int y = (int) size.getHeight() - lineHeight * 3 - 25 - i
							* unitHeight;
					g.drawLine(0, y, (int) size.getWidth(), y);
					g.drawString(String.valueOf(i), 10, y - lineHeight);
				}

				for (int i = 0; i < n; ++i) {
					int left = blockWidth * i, right = blockWidth * (i + 1), blockHeight = unitHeight
							* datos.get(i).second;

					g.setColor(new Color(120, 105, 242));
					g.fillRect((right + left - 50) / 2, (int) (size.getHeight()
							- blockHeight - lineHeight * 3 - 25), 50,
							blockHeight);
					g.setColor(Color.BLACK);
					g.drawRect((right + left - 50) / 2, (int) (size.getHeight()
							- blockHeight - lineHeight * 3 - 25), 50,
							blockHeight);

					{
						String nombre = datos.get(i).first.getDescripcion();
						Rectangle2D rc = g.getFontMetrics().getStringBounds(
								nombre, g);
						g.drawString(nombre,
								(int) ((right + left - rc.getWidth()) / 2),
								(int) (size.height - lineHeight * 2 - 20));
					}

					{
						String nombre = "("
								+ String.valueOf(datos.get(i).second) + ")";
						Rectangle2D rc = g.getFontMetrics().getStringBounds(
								nombre, g);
						g.drawString(nombre,
								(int) ((right + left - rc.getWidth()) / 2),
								(int) (size.height - lineHeight - 10));
					}
				}

			}
		}
	};

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JPanel jPanel1;
	private JLabel lblHasta;
	private JLabel lblDesde;
	private JRadioButton rbFecha;
	private JButton btConsultar;
	private JLabel lblFiltros;
	private JDateChooser dcHastaFechaInicio;
	private JDateChooser dcDesdeFechaInicio;
	private JButton btSalir;
	private Chart chart;

	public DlgReporteTiposServicio(JFrame frame) {
		super(frame);
		initGUI();
	}

	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				this.setTitle("Reporte Estadistico por Tipo de Servicios Finalizados");
				{
					jPanel1 = new JPanel();
					getContentPane().add(jPanel1);
					jPanel1.setBounds(33, 38, 625, 107);
					jPanel1.setBorder(BorderFactory
							.createEtchedBorder(BevelBorder.LOWERED));
					jPanel1.setLayout(null);
					{
						btConsultar = new JButton();
						jPanel1.add(btConsultar);
						btConsultar.setText("Consultar");
						btConsultar.setIcon(new ImageIcon(getClass()
								.getClassLoader().getResource(
										"iconos/report.png")));
						btConsultar.setHorizontalTextPosition(JButton.CENTER);
						btConsultar.setVerticalTextPosition(JButton.BOTTOM);
						btConsultar.setBounds(496, 20, 106, 40);
					}
					{
						rbFecha = new JRadioButton();
						jPanel1.add(rbFecha);
						rbFecha.setText("Fecha de Finalizacion:");
						rbFecha.setBounds(21, 25, 181, 18);
						rbFecha.setFont(new java.awt.Font("Tahoma", 1, 11));
					}
					{
						lblDesde = new JLabel();
						jPanel1.add(lblDesde);
						lblDesde.setText("Desde:");
						lblDesde.setBounds(21, 63, 45, 14);
						lblDesde.setFont(new java.awt.Font("Tahoma", 1, 11));
					}
					{
						dcDesdeFechaInicio = new JDateChooser();
						jPanel1.add(dcDesdeFechaInicio);
						dcDesdeFechaInicio.setBounds(66, 56, 130, 28);
						dcDesdeFechaInicio.setDate(new Date());
						dcDesdeFechaInicio.setEnabled(false);
					}
					{
						lblHasta = new JLabel();
						jPanel1.add(lblHasta);
						lblHasta.setText("Hasta:");
						lblHasta.setBounds(202, 63, 43, 14);
						lblHasta.setFont(new java.awt.Font("Tahoma", 1, 11));
					}
					{
						dcHastaFechaInicio = new JDateChooser();
						jPanel1.add(dcHastaFechaInicio);
						dcHastaFechaInicio.setBounds(245, 56, 128, 28);
						dcHastaFechaInicio.setDate(new Date());
						dcHastaFechaInicio.setEnabled(false);
					}
					{
						btSalir = new JButton();
						getContentPane().add(btSalir);
						btSalir.setText("Salir");
						btSalir.setIcon(new ImageIcon(getClass()
								.getClassLoader().getResource(
										"iconos/Salir.gif")));
						btSalir.setHorizontalTextPosition(JButton.CENTER);
						btSalir.setVerticalTextPosition(JButton.BOTTOM);
						btSalir.setBounds(595, 440, 79, 40);
					}
				}
				{
					lblFiltros = new JLabel();
					getContentPane().add(lblFiltros);
					lblFiltros.setText("Filtros:");
					lblFiltros.setBounds(39, 18, 58, 14);
					lblFiltros.setFont(new java.awt.Font("Tahoma", 1, 11));
				}
				{
					chart = new Chart();
					chart.actualizar(null, null);
					getContentPane().add(chart);
					chart.setBounds(33, 172, 625, 254);
				}
			}
			this.setSize(708, 539);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private DlgReporteTiposServicio dspres = this;

	public void mostrar() {
		setLocationRelativeTo(null);
		setVisible(true);

		btSalir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dspres.dispose();
			}
		});

		rbFecha.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				dcDesdeFechaInicio.setEnabled(rbFecha.isSelected());
				dcHastaFechaInicio.setEnabled(rbFecha.isSelected());
			}
		});

		btConsultar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (rbFecha.isSelected()) {
					if (dcDesdeFechaInicio.getDate().compareTo(
							dcHastaFechaInicio.getDate()) > 0) {
						Date d = dcDesdeFechaInicio.getDate();
						dcDesdeFechaInicio.setDate(dcHastaFechaInicio.getDate());
						dcHastaFechaInicio.setDate(d);
					}

					chart.actualizar(dcDesdeFechaInicio.getDate(),
							dcHastaFechaInicio.getDate());
				} else
					chart.actualizar(null, null);
				
				chart.repaint();
			}
		});
	}

}
